package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.domain.Department;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.qo.DepartmentQo;
import cn.wolfcode.rbac.service.DepartService;
import cn.wolfcode.rbac.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author xiahuihui
 * #Description DepartmentController
 * #Date: 2022/7/2 13:22
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartService departService;

    //查询部门
    @RequestMapping("/list")
    @RequiredPermission({"部门列表","permission:list"})
    public String departmentList(Model model,@ModelAttribute("qo") DepartmentQo qo){
        //分页查询
        PageInfo<Department> pageResult = departService.findByPage(qo);
        //List<Department> departmentAll = departService.findDepartmentAll();
        model.addAttribute("pageResult",pageResult);
        return "department/list";
    }

    //编辑部门
    @RequestMapping("/input")
    @RequiredPermission({"部门编辑","permission:input"})
    public String addDepartment(Model model , Long id){
        if(id != null){
            //根据id查询到对应的员工信息然后进行回显
            Department department = departService.findById(id);
            model.addAttribute("department",department);

        }
        return "department/input";
    }

    //保存部门
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"部门保存或更新","permission:saveOrUpdate"})
    public String saveDepartment(Department department){
        departService.saveDepartment(department);
        return "redirect:list";
    }

    //删除部门
    @RequestMapping("/delete")
    @RequiredPermission({"部门删除","permission:delete"})
    public String deleteDepartment(Long id){
        departService.deleteDepartment(id);
        return "redirect:list";
    }
}
